/**
 * @author thaodo
 *
 *
 */
package com.mfsoft.model;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;

//adad
public class ImageItem implements Parcelable {
	private String imageUrl;
	private String imagePath;
	private String name;
	private String size;
	private String date;

	public ImageItem() {
		imageUrl = imagePath = name = size = date = "";
	}

	public ImageItem(String imageUrl, String name, String size, String date,
			String imagePath) {
		this.imageUrl = imageUrl;
		this.imagePath = imagePath;
		this.name = name;
		this.size = size;
		this.date = date;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String toString() {
		return getName() + ":" + getSize() + ":" + getImageUrl() + ":"
				+ getImagePath() + ":" + getDate();
	}

	public Bitmap decodeSampleBitmapFromUri(String path, int reqWidth,
			int reqHeight) {
		Bitmap bm = null;
		final BitmapFactory.Options options = new BitmapFactory.Options();
		// check do phan giai
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		// tinh sampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// decode bitmap voi sample size
		options.inJustDecodeBounds = false;
		bm = BitmapFactory.decodeFile(path, options);

		return bm;
	}

	public int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// width va height cua anh goc
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			final int halfHeight = height/2;
			final int halfWidth = width/2;
			while ((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize)>reqWidth) {
				inSampleSize *=2;
			}
		}

		return inSampleSize;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int arg1) {
		dest.writeString(imageUrl);
		dest.writeString(imagePath);
		dest.writeString(name);
		dest.writeString(size);
		dest.writeString(date);

	}

	private ImageItem(Parcel in) {
		// TODO Auto-generated method stub
		this.imageUrl = in.readString();
		this.imagePath = in.readString();
		this.name = in.readString();
		this.size = in.readString();
		this.date = in.readString();

	}

	public static final Creator<ImageItem> CREATOR = new Creator<ImageItem>() {

		@Override
		public ImageItem[] newArray(int size) {
			// TODO Auto-generated method stub
			return new ImageItem[size];
		}

		@Override
		public ImageItem createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new ImageItem(source);
		}
	};
}
